package com.feiyu.order.controller;

import com.feiyu.common.utils.R;
import com.feiyu.order.dto.createOrderDto;
import com.feiyu.order.dto.operateOrderDto;
import com.feiyu.order.service.OrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: Doate
 * @Date: Created in 2021/4/16 1:25
 * @Description:
 */

@RestController
public class OrderOperateController {

    @Resource
    OrderService orderService;

    @PostMapping("/add")
    public R createOrder(@RequestBody createOrderDto dto, @RequestHeader("userPower") int power) {

        if (power != 10) {
            return R.error();
        }
        Map<String, Object> data = orderService.createOrder(dto);
        return R.ok().put("data",data);
    }

    @PostMapping("/receive")
    public R receive(@RequestBody operateOrderDto dto, @RequestHeader("userPower") int power, @RequestHeader("userId") int userId) {

        if (power == 10) {
            orderService.buyerReceiveGoods(dto,userId);
        } else if (power >10 && power <14) {
            orderService.shopReceiveGoods(dto,userId);
        }

        return R.ok();
    }

    @PostMapping("/reback")
    public R rebackGoods(@RequestBody operateOrderDto dto, @RequestHeader("userPower") int power) {

        if (power != 10) {
            return R.error("仅限买家返还租赁物");
        }
        orderService.rebackGoods(dto.getOrderId());

        return R.ok();
    }

    @PostMapping("/changeToBuy")
    public R changeToBuy(@RequestBody operateOrderDto dto, @RequestHeader("userPower") int power, @RequestHeader("userId") int userId) {

        if (power != 10) {
            return R.error();
        }
        if (!orderService.changeToBuy(dto.getOrderId())) {
            return R.error("该商品不允许转租为买");
        }

        return R.ok();
    }

    @PostMapping("/refund")
    public R refund(@RequestBody operateOrderDto dto, @RequestHeader("userPower") int power, @RequestHeader("userId") int userId) {

        if (power != 10) {
            return R.error("只有买家账户可以操作");
        }
        if (!orderService.refund(dto.getOrderId())) {
            return R.error("该订单未处于不可签收状态");
        }
        return R.ok();
    }

    @PostMapping("/shipAgain")
    public R shipAgain(@RequestBody operateOrderDto dto, @RequestHeader("userPower") int power, @RequestHeader("userId") int userId) {

        if (power != 10) {
            return R.error("只有买家账户可以操作");
        }
        if (!orderService.shipAgain(dto.getOrderId())) {
            return R.error("该订单未处于不可签收状态");
        }

        return R.ok();
    }

    @PostMapping("/test")
    public void test() {
        orderService.add();
    }


}
